<?php
echo '<center>"time" repair (for News ONLY) script by fishMD<br>tested on 1.2.4</center><hr>';

function start()
{
	echo '<center><h2><font color=red>1. MAKE BACKUP FOR FOLDER "reloadCMS_root_folder/content/a" !!!<br>2. MAKE SHURE THAT THIS FILE PLACED IN ROOT FOLDER OF ReloadCMS !!!</font></h2><br><form name="start" methos=GET action="'.$_SERVER["SCRIPT_NAME"].'"><input type=hidden name="action" value="start"><input type=submit value="START REPAIR">';
}

function change()
{
}

function search($dir='content/a/news')
{
	if ($dh = opendir($dir)) 
	{
        	while (($file = readdir($dh)) !== false) 
        	{
        		if ((is_dir($dir.'/'.$file)) AND (($file !== '.') AND ($file !== '..')))
        		{
        			search($dir.'/'.$file);
            		}
            		if (is_file($dir.'/'.$file))
            		{
            			if ($file=='define')
            			{
            				$lines = file_get_contents($dir.'/'.$file);
            				$lines2 = file($dir.'/'.$file);
            				if (strstr($lines, '" = ""'))
            				{
            					unset ($lines2[5]);
            					$handle = fopen($dir.'/'.$file, 'w');
            					foreach ($lines2 as $value)
            					{
            						fwrite($handle,$value);
            					}
            					fclose($handle);
            					$ini_array = parse_ini_file($dir.'/'.$file);
            					echo '/'.$dir.'/'.$file.' ('.$ini_array['title'].') - <b><font color=red>repaired</font></b><br>';
            				}
            				else
            				{
            					$ini_array = parse_ini_file($dir.'/'.$file);
            					echo '/'.$dir.'/'.$file.' ('.$ini_array['title'].') - <font color=blue>good</font><br>';
            				}
            				
            			}
            		}
        	}
        	closedir($dh);
    	}

}

function repair_i($dir='content/a/news')
{
	$r=array();
	$dh = opendir($dir);
	while (($file = readdir($dh)) !== false) 
        	{
        		if ((is_dir($dir.'/'.$file)) AND (($file !== '.') AND ($file !== '..')))
        		{
        			$dh2 = opendir($dir.'/'.$file);
        			while (($file2 = readdir($dh2)) !== false) 
        			{
        				if ((is_dir($dir.'/'.$file.'/'.$file2)) AND (($file2 !== '.') AND ($file2 !== '..')))
        				{
        					$r2=array();
        					$ini_array = parse_ini_file($dir.'/'.$file.'/'.$file2.'/define');
        					$cc=0;
        					if (file_exists($dir.'/'.$file.'/'.$file2.'/comments')) {$cc=date("U", filectime($dir.'/'.$file.'/'.$file2.'/comments'));}
        					$r2[]=array('time'=>$ini_array['time'], 'ccnt'=>$ini_array['comcount'], 'view'=>$ini_array['views'], 'lcnt'=>$cc);
        					$r[$file][$file2]=$r2[0];
        				}
        			}
        			closedir($dh2);
        		}
        	}
        closedir($dh);
        $si=serialize($r);
        echo '<textarea cols=80 rows=10 readonly>'.$si.'</textarea>';
        $f=fopen('content/a/news/index','w');
        fwrite($f,$si);
        fclose($f);
}

if (!isset($_GET['action']))
	{
		global $ind;
		start(); 
	}
else
	{
		$ind=array();
		echo '<h2>1. Repairing INI files...</h2>';
		search();
		echo '<h2>2. Building indexes...</h2>';
		repair_i();
		echo '<br>Saved to "content/a/news/index"';
		echo '<h2>DONE</h2>';
	}
echo '<hr><center><a href="http://reloadcms.com">reloadcms.com</a></center>'
?>